package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.Button;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GoodsAddressListBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.inteface.CitySelectInterface;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.UserInfoItem;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.Arrays;
import java.util.List;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/11 9:52
 * desc   :新增收货地址
 */
public class AddAddressActivty extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private UserInfoItem mItemName;
    private UserInfoItem mItemPhone;
    private UserInfoItem mItemCity;
    private UserInfoItem mItemDetailsRoad;
    private Button mBtnGetCode;

    private String countyidStr;//省id
    private String cityidStr;//市id
    private String zoneidStr;//区id
    private boolean addAddress;
    private String modifyId;
    private String proviceName = "";
    private String cityName = "";
    private String countryName = "";

    @Override
    public int getLayout() {
        return R.layout.activity_add_address;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mItemName = (UserInfoItem) findViewById(R.id.item_name);
        mItemPhone = (UserInfoItem) findViewById(R.id.item_phone);
        mItemCity = (UserInfoItem) findViewById(R.id.item_city);
        mItemDetailsRoad = (UserInfoItem) findViewById(R.id.item_details_road);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);

    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        addAddress = intent.getBooleanExtra("add_address", true);
        mItemPhone.getEditText().setInputType(InputType.TYPE_CLASS_PHONE);

        mActivityTitle.setTvTitle(addAddress ? getString(R.string.string_drawaerlayout_shouhuo) : getString(R.string.string_editor_address));
        if (!addAddress) {
            GoodsAddressListBean.DataBean.ResBean bean = (GoodsAddressListBean.DataBean.ResBean) intent.getSerializableExtra("address_modify");
            initUI(bean);
        }
    }

    //修改
    private void initUI(GoodsAddressListBean.DataBean.ResBean bean) {
        setText(mItemName.getEditText(), BaseMethod.notNullDefaultValueNull(bean.getName()));
        setText(mItemPhone.getEditText(), BaseMethod.notNullDefaultValueNull(bean.getTelephone()));
        String address_prefix = bean.getAddress_prefix();
        setText(mItemCity.getTextView(), BaseMethod.notNullDefaultValueNull(address_prefix));
        setText(mItemDetailsRoad.getEditText(), BaseMethod.notNullDefaultValueNull(bean.getAddress_postfix()));
        zoneidStr = bean.getZone_id();
        cityidStr = bean.getCity_id();
        countyidStr = bean.getCounty_id();
        modifyId = bean.getId() + "";
        if (BaseMethod.notNull(address_prefix) && address_prefix.contains(" ")) {
            List<String> list = Arrays.asList(address_prefix.split(" "));
            if (list != null && list.size() == 3) {
                proviceName = list.get(0);
                cityName = list.get(1);
                countryName = list.get(2);
            }
        }
    }


    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mItemCity.setItemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseMethod.cityPicker(AddAddressActivty.this, mItemCity.getTextView(), proviceName, cityName, countryName, zoneidStr, cityidStr, countyidStr, new CitySelectInterface() {
                    @Override
                    public void getId(String zoneid, String cityid, String countyid) {
                        countyidStr = countyid;
                        cityidStr = cityid;
                        zoneidStr = zoneid;
                    }
                });
            }
        });
        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //提交xinzeng
                if (addAddress) {
                    save();
                } else {
                    modify();
                }
            }
        });
    }

    //新增
    private void save() {
        String name = mItemName.getEtContent();
        String phone = mItemPhone.getEtContent();
        String city = mItemCity.getTvContent();
        String details = mItemDetailsRoad.getEtContent();
        boolean noEmpty = noEmpty(name, phone, city, details);
        if (noEmpty) {
            showLoadingDialog("");
            PhoneLive.addGoosAddress(getUserToken(), name, phone, countyidStr, cityidStr, zoneidStr, details, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    hideLoadingDialog();
                    ToastUtils.showText(e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    hideLoadingDialog();
                    if (BaseUtils.isJSONValid(response)) {
                        try {
                            if (BaseUtils.codeIs0(mContext, response)) {
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        ToastUtils.showText(getString(R.string.string_response_error));
                    }
                }
            });
        }
    }

    //修改
    private void modify() {
        String name = mItemName.getEtContent();
        String phone = mItemPhone.getEtContent();
        String city = mItemCity.getTvContent();
        String details = mItemDetailsRoad.getEtContent();
        boolean noEmpty = noEmpty(name, phone, city, details);
        if (noEmpty) {
            showLoadingDialog("");
            PhoneLive.modifyGoosAddress(getUserToken(), modifyId, name, phone, countyidStr, cityidStr, zoneidStr, details, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    hideLoadingDialog();
                    ToastUtils.showText(e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    hideLoadingDialog();
                    if (BaseUtils.isJSONValid(response)) {
                        try {
                            if (BaseUtils.codeIs0(mContext, response)) {
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        ToastUtils.showText(getString(R.string.string_response_error));
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    private boolean noEmpty(String name, String phone, String city, String details) {
        if (BaseMethod.isNull(name)) {
            ToastUtils.showText(getString(R.string.string_address_name_null));
            return false;
        } else if (BaseMethod.isNull(phone)) {
            ToastUtils.showText(getString(R.string.string_address_phone_null));
            return false;
        } else if (BaseMethod.isNull(city)) {
            ToastUtils.showText(getString(R.string.string_address_city_null));
            return false;
        } else if (BaseMethod.isNull(details)) {
            ToastUtils.showText(getString(R.string.string_address_details_null));
            return false;
        } else
            return true;

    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "新增收货地址界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "新增收货地址界面");
    }
}
